package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * 检查祖服务
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
   @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 批量导入预约设置
     * @param list
     */
    @Override
    public void add(List<OrderSetting> list) {
        if(list !=null && list.size()>0){
            for (OrderSetting orderSetting : list) {
                //检查数据(日期)是否存在
                long count=orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(count>0) {
                    //若存在执行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    //不存在,执行添加操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    /**
     * 获取预约设置
     * @param date
     * @return List<map>
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {

//        return orderSettingDao.findCountByOrderDate(date);

        String begin=date+"-1";
        String end=date+"-31";
        Map<String, String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        //调用dao,根据日期范围查询预约设置数据
        List<OrderSetting> list=orderSettingDao.getOrderSettingByMonth(map);
        List<Map> result =new ArrayList<>();
     if (list !=null && list.size()>0) {
         for (OrderSetting orderSetting : list) {
             Map<String, Object> m = new HashMap<>();
             //获取日期数字(几号)
             m.put("date",orderSetting.getOrderDate().getDate());
             m.put("number",orderSetting.getNumber());
             m.put("reservations",orderSetting.getReservations());

             result.add(m);
         }
     }
        return result;
    }

    /**
     * 编辑预约设置
     * @param orderSetting
     */
    @Override
    public void editOrderSettingByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        //根据日期查询是否已经进行了预约设置
        long count = orderSettingDao.findCountByOrderDate(orderDate);
        if(count>0) {
            //若存在执行更新操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            //不存在,执行添加操作
            orderSettingDao.add(orderSetting);
        }
    }
}
